home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / scm / wb1a1.lha / wb / dbscm.doc < prev    next >
Encoding:
Text File  |  1993-06-29  |  10.1 KB  |  268 lines

  1.  WB-tree File Based Associative String Data Base System.
  2.  Copyright (c) 1991, 1992, 1993 Holland Mark Martin
  3.  
  4. Permission to use, copy, modify, and distribute this software and its
  5. documentation for educational, research, and non-profit purposes and
  6. without fee is hereby granted, provided that the above copyright
  7. notice appear in all copies and that both that copyright notice and
  8. this permission notice appear in supporting documentation, and that
  9. the name of Holland Mark Martin not be used in advertising or
  10. publicity pertaining to distribution of the software without specific,
  11. written prior consent in each case.  Permission to incorporate this
  12. software into commercial products can be obtained from Jonathan
  13. Finger, Holland Mark Martin, 174 Middlesex Turnpike, Burlington, MA,
  14. 01803-4467, USA.  Holland Mark Martin makes no representations about
  15. the suitability or correctness of this software for any purpose.  It
  16. is provided "as is" without express or implied warranty.  Holland Mark
  17. Martin is under no obligation to provide any services, by way of
  18. maintenance, update, or otherwise.
  19.  
  20.                 DBSCM
  21.  
  22. DBSCM is a disk based, sorted associative array package (WB)
  23. integrated into the Scheme implementation SCM.  These associative
  24. arrays consist of keys and values which are strings of length less
  25. than 256 bytes.  Associative arrays can be used to form a MUMPS style
  26. database which ncan be used to implement standard record structures
  27. without auxiallary files (see example in example.scm).
  28.  
  29. Basic operations are creation, destruction, opening and closing of
  30. diskfiles and arrays, insertion, deletion, retrieval, successor, and
  31. predecessor (with respect to dictionary order of keys).  Functional
  32. application of find-next, deletion, and modification over a range of
  33. consecutive key values is supported.
  34.  
  35. Multiple associative arrays can be stored in one disk file.
  36. Simultaneous access to multiple disk files is supported.  A structure
  37. checker, garbage collector are included.  A repair program and
  38. ram-disk type file (for temporary structures) are in developement.
  39.  
  40. The current WB implementation has a file size limit of 2^32 * block
  41. size (default 2048) = 2^43 bytes (8796 Gbytes).  We currently run WB
  42. with file sizes of several hundred Megabytes.  WB does its own memory
  43. and disk management and maintains an in core cache of recently used
  44. blocks.  The WB implementation adds about 45 Kbytes to the size of
  45. SCM.
  46.  
  47. WB is implemented using a B-tree variant.  B-trees give slower access
  48. than hashing but are dynamic and provide an efficient determination of
  49. successor and predecessor keys.  All operations are O(log(n)) in the
  50. size of the database.  B-trees are commonly used by database systems
  51. for implementing index structures.  B-trees are optimized for using
  52. the minimum number of disk operations for large data structures.
  53. Prefix and suffix compression are used for storage efficiency.
  54.  
  55.                  STATUS CODES
  56.  
  57. Some of the following calls return status codes defined as follows:
  58.  
  59. (define SUCCESS 0)    ; successful execution
  60. (define NOTPRES -1)    ; successful execution, no data present or no change made
  61. (define TERMINATED -2)    ; failure, no damage, caller can retry operation
  62. (define RETRYERR -10)    ; failure, no damage, caller can retry operation
  63. (define ARGERR -15)    ; failure, no damage, call was in error
  64. (define NOROOM -20)    ; failure, no damage, out of room in file
  65. (define TYPERR -30)    ; failure, file or object was not of correct type
  66. (define IOERR -40)    ; i/o error, DB may be damaged
  67. (define STRANGERR -45)    ; internal error, DB may be damaged
  68. (define UNKERR -90)    ; placeholder code
  69.  
  70.                    SEGMENTS
  71.  
  72.    (init-wb max-num-ents max-num-buks max-blk-size)    procedure
  73.  
  74. Initializes the WB system.  Max-blk-size determines the size of the
  75. disk cache buffers.  Max-num-ents is the number of disk cache buffers
  76. to be allocated.  (* max-num-ents max-blk-size) should be less than
  77. the size of RAM on your computer.  If not all max-num-ents cannot be
  78. allocated (by malloc) then WB can still run properly.  Max-num-buks is
  79. the number of hash buckets for the disk cache.  It should be of
  80. approximately the same size as or larger than max-num-ents.  The
  81. number of buffers actually allocated is returned.
  82.  
  83. If open-seg or create-seg is called when init-wb is not called, then
  84. WB will be initialized as though init-wb was called with the
  85. parameters 75, 150, and 2048.
  86.  
  87.    (final-wb)                        procedure
  88.  
  89. Frees all memory used by the WB system.  All segments will be closed.
  90.  
  91.    (open-seg seg filename mode)                procedure
  92.  
  93. Seg should be a non-negative number.  Opens the database seg in
  94. filename and returns seg if successful, a status code otherwise.  The
  95. database seg will be read-only if the mode argument is 0.  It will be
  96. read-write if the mode argument is 2.
  97.  
  98.    (close-seg seg hammer)                procedure
  99.  
  100. Closes database segment seg and the file containing it.  If hammer is
  101. #f then if there are any problems freeing buffers then the close is
  102. aborted.  A status code is returned.
  103.  
  104.    (make-seg seg filename block-size)            procedure
  105.  
  106. Makes a new empty database seg of name filename.  A status code is
  107. returned.  Use open-seg to get the new seg.  The block-size is the
  108. size of B-tree blocks.  Unless you are testing splitting algorithms,
  109. block-size should be 2048.
  110.  
  111.               ASSOCIATIVE ARRAYS
  112.  
  113. In this group of functions, typ should be either #\D (directory) or
  114. #\T (regular tree).  B-trees with typ #\D which are pointed to by
  115. special entries in the root block (1) protect all their special
  116. entries from garbage collection by the check program.  #\T is for
  117. regular (data) arrays.
  118.  
  119.    (create-db seg typ name)                procedure
  120.  
  121. Returns a B-tree whose name has been entered in the root directory.
  122.  
  123.    (open-db seg name)                    procedure
  124.  
  125. Returns the B-tree whose name has been entered in the root directory
  126. or #f if not found
  127.  
  128.             LOWER LEVEL OPERATIONS
  129.  
  130. The write-control-bits argument (wcb) to these functions controls the
  131. latency of updates to the file after various operations.  These bits
  132. are defined as follows:
  133.  
  134. (1) SAP: save block after PUTs
  135. (2) SAR: save block after REMOVEs
  136. (4) SAC: force block save after cached block changes
  137. (8) FAC: flush buffer entirely after cached block changes
  138.     (not currently implemented)
  139.  
  140.    (create-bt seg typ wcb)                procedure
  141.  
  142. Creates a new root block in seg seg of type typ and returns a
  143. bt-handle open to it.  This would typically be used to create a
  144. temporary b-tree which should be reclaimed by check if system crashes.
  145.  
  146.    (open-bt seg blknum wcb)                procedure
  147.  
  148. Returns a bt-handle open to seg number seg, block number blknum.  If
  149. no such block exists or is not a root block a status code is returned.
  150.  
  151.    (str2long string index)                procedure
  152.  
  153. Block numbers and some other integer quantities are stored in the
  154. database as four-byte pointers.  Str2long converts the 4 bytes in
  155. string starting at index into an unsigned integer.
  156.  
  157.    (long2str! string index integer)            procedure
  158.  
  159. Stores integer into 4 bytes of string starting at index.
  160.  
  161.               RECORD OPERATIONS
  162.  
  163.    (bt:get han key)                    procedure
  164.  
  165. Returns a string of the value associated with key in the bt which han
  166. is open to.  Returns #f if key is not in the bt.  Key is a string.
  167.  
  168.    (bt:next han key)                    procedure
  169.  
  170. Returns the next key in bt han or #f if none.
  171.  
  172.    (bt:prev han key)                    procedure
  173.  
  174. Returns the previous key in bt han or #f if none.
  175.  
  176.    (bt:put! han key val)                procedure
  177.  
  178. Associates key with val in the bt han.  A status code is returned.
  179.  
  180.    (bt:rem! han key)                    procedure
  181.  
  182. Removes key and it's associated value from bt han.
  183.  
  184.              SEMAPHORE OPERATIONS
  185.  
  186. These 2 calls can be used for locking and synchronizing processes.
  187.  
  188.    (bt:put han key val)                    procedure
  189.  
  190. Associates key with val in the bt han only if key was previously
  191. empty.  Returns #t for success, #f for failure.
  192.  
  193.    (bt:rem han key)                    procedure
  194.  
  195. Removes key and it's associated value from bt han only if key is
  196. present.  Returns key's value for success, #f for failure (not
  197. present).
  198.  
  199.              MULTIPLE OPERATIONS
  200.  
  201.    (bt:rem* han key1 key2)                procedure
  202.  
  203. Removes keys (and their associated values) between (including) key1
  204. and (not including) key2 from bt han.  A status code is returned.
  205.  
  206.    (bt:scan han op key1 key2 func blklimit)        procedure
  207.  
  208. Applies procedure func to a range of keys (and values) and either
  209. counts, modifies, or deletes those associations.  A list of a status
  210. code, the count of records processed, and a new value for key1 is
  211. returned.
  212.  
  213. If op is -1, indicated keys will be deleted;  If op is 0, indicated
  214. keys will be counted;  If op is 1, the value of each key in the range
  215. will be changed to be the string returned by func.
  216.  
  217. Func is called with 2 string arguments, the key and the value.  If op
  218. is 1 and FUNC returns #f then no change will be made.  If op is 1 and
  219. func returns a string then that string will replace the value for that
  220. key.  For the other (count, delete) modes, func should return #f or
  221. #t.  If func is #t, the association will be counted (and possibly
  222. deleted).
  223.  
  224. Keys from key1 (inclusive) up to key2 (exclusive) are scanned.  If
  225. blklimit is -1 then the entire range is scanned; otherwise only as
  226. many blocks (internal database structures) as specified by blklimit
  227. are scanned.  The scan can be restarted by using the returned
  228. information.  For instance, the following expression counts the number
  229. of keys between "3" and "4" one block at a time and returns a list of
  230. the number of keys and the number of blocks.
  231.  
  232. (do ((res (bt:scan current-bt 0 "3" "4" (lambda (k v) #t) 1)
  233.       (bt:scan current-bt 0 (caddr res) "4" (lambda (k v) #t) 1))
  234.      (blks 0 (+ 1 blks))
  235.      (cnt 0 (+ cnt (cadr res))))
  236.     ((not (= -1 (car res))) (list (+ cnt (cadr res)) (+ 1 blks))))
  237.  
  238. It is good to specify a positive blklimit when dealing with large
  239. scans.  More details on the operation of scan can be found in
  240. scan.scm.
  241.  
  242.                  DIAGNOSTICS
  243.  
  244. The value returned by the following routines is unspecified.
  245.  
  246.    (check-access)
  247.  
  248. Checks that buffers and locks are in a consistent state and fixes them
  249. if WB routines have been interrupted.
  250.  
  251.    (clear-stats)
  252.  
  253. Resets all the collected statistics to 0.
  254.  
  255.    (stats)
  256.  
  257. Prints a summary of operational statistics since the last clear-stats
  258. or cstats.
  259.  
  260.    (cstats)
  261.  
  262. Prints a summary of operational statistics since the last clear-stats
  263. or cstats, then calls clear-stats.
  264.  
  265.    (show-buffers)
  266.  
  267. Prints a table of the status of all disk cache buffers.
  268.